//
//  BaseViewController.m
//  PandaTakeaway
//
//  Created by smufs on 2017/5/17.
//  Copyright © 2017年 李冰. All rights reserved.
//

#import "BaseViewController.h"

@interface BaseViewController ()

@end

@implementation BaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = kWhiteColor;
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, -64, lbScreenWidth, 64)];
    [self.view addSubview:view];
    view.backgroundColor = kWhiteColor;
    self.style = ViewControllerStyleDefault;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    for (UIView * view in self.view.subviews) {
        if ([view isKindOfClass:[UITextField class]]) {
            UITextField * tf = (UITextField *)view;
            [tf resignFirstResponder];
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setStyle:(ViewControllerStyle)style {
    
    _style = style;
    
    if (style == ViewControllerStyleDefault)
    {
        
    }
    else if (style == ViewControllerStyleLight)
    {
        
        [self setWhiteTitleViewItem:self.title];
        [self setWhiteStyleBackBarItem];
    }
}

- (void)setWhiteStyleBackBarItem {
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"shangjiaxinxi_fanhui"] forState:UIControlStateNormal];
    button.lb_size = CGSizeMake(30, 30);
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
}

- (void)setWhiteTitleViewItem:(NSString *)title {
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 20)];
    label.textColor = kWhiteColor;
    
    label.text = title;
    self.navigationItem.titleView = label;
}

- (void) back {
    
    if (self.navigationController.viewControllers.firstObject == self)
    {
        
        [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    }
    else if (self.navigationController)
    {
        
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)setTitle:(NSString *)title {
    
    [super setTitle:title];
    
    if (self.style == ViewControllerStyleLight)
    {
        
        [self setWhiteTitleViewItem:title];
    }
}

/** 设置标题*/
- (void)addTitleViewWithTitle:(NSString *)title{
    
    UILabel * titleView = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
    titleView.text = title;
    titleView.textAlignment = NSTextAlignmentCenter;
    titleView.font = [UIFont boldSystemFontOfSize:17];
    self.navigationItem.titleView = titleView;
    titleView.textColor = kBlackColor;
}

/** 设置BarButtonItem*/
- (void)customBarButtonItemWithImg:(NSString *)img action:(SEL)action isLeft:(BOOL)isLeft
{
    // 设置按钮
    if (img) {
        UIBarButtonItem * item = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:img] style:UIBarButtonItemStylePlain target:self action:action];
        if (isLeft) {
            self.navigationItem.leftBarButtonItem = item;
        }else{
            self.navigationItem.rightBarButtonItem = item;
        }
    }
}

- (void)customBarButtonItemWithTitle:(NSString *)title action:(SEL)action isLeft:(BOOL)isLeft color:(UIColor *)color {
    // 设置按钮
    UIButton *btn = [UIButton new];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:color forState:UIControlStateNormal];
    btn.titleLabel.font = lbFont(16);
    [btn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    [btn sizeToFit];
    UIBarButtonItem * item = [[UIBarButtonItem alloc]initWithCustomView:btn];
    if (isLeft) {
        self.navigationItem.leftBarButtonItem = item;
    }else{
        self.navigationItem.rightBarButtonItem = item;
    }
}

- (void)dealloc {
    LBLog(@"%@ dealloc!", self);
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    
    return UIBarStyleDefault;
}

@end
